import 'package:common_utils/common_utils.dart';
import 'package:dss_base/dss_base.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_demo_test/data/bean/goods_bean.dart';
import 'package:flutter_demo_test/data/respository/test_repository.dart';

class NetCPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _NetCPageState();
}

class _NetCPageState extends BasePageState<NetCPage, NetBViewModel> {
  @override
  bool get enableLoadMore => true;

  @override
  NetBViewModel onCreateViewModel() => NetBViewModel();

  @override
  PreferredSizeWidget? onCreateAppbar(BuildContext context) {
    return AppBarX(title: '需要初始化数据', onBackPressed: ()=> NavigatorUtil.back(),);
  }

  @override
  Widget onCreateSuccessPage(BuildContext context, ScrollPhysics? physics) {
    return ListView.builder(
      physics: physics,
      itemCount: viewModel.pageData?.length,
      itemBuilder: (BuildContext context, int index) {
        return SizedBox(
          width: double.infinity,
          height: 100,
          child: Center(
            child: Text(viewModel.pageData?[index].goodsName ?? ''),
          ),
        );
      },
    );
  }
}

class NetBViewModel extends BasePageViewModel<List<GoodsBean>> {
  @override
  bool onCheckEmpty(BaseEntity entity, List<GoodsBean>? data) =>
      data == null || data.isEmpty;

  @override
  List<GoodsBean>? onConvertPageData(dynamic json) => JsonUtil.getList(json);

  @override
  Future<BaseEntity> onRequestPageData(int pageIndex) =>
      TestRepository().getTest(pageIndex);

  @override
  void onSuccess(BaseEntity entity, List<GoodsBean>? data) {}

  @override
  void onSuccessMore(BaseEntity entity, List<GoodsBean>? data) {
    if (data == null || data.isEmpty) return;
    if (pageData == null) {
      pageData = [];
    }
    pageData?.addAll(data);
  }
}
